package com.kd.struts.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport {
	private File upload;
	private String uploadFileName;
	private String uploadContentType;
	
	
	@Override
	public String execute() throws Exception {
		FileInputStream inputStream=null;
		OutputStream outputStream= null;
		String realPath=ServletActionContext.getServletContext().getRealPath("/uploads");
		try{
			System.out.println(uploadFileName);
			inputStream = new FileInputStream(upload);
			outputStream = new FileOutputStream(realPath+"\\"+uploadFileName);
			byte buffer[] = new byte[1024];
			int count = 0 ;
			while((count = inputStream.read(buffer))>0){
				outputStream.write(buffer,0,count);
			}
			return SUCCESS;
		}catch(Exception e){
			e.printStackTrace();
			return "false";
		}finally{
			outputStream.close();
			inputStream.close();
		}
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public String getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
}
